package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsCategory;
import com.qf.service.CategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author Wy
 * @version V1.0
 * @Project MavenDemo
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/6/21 11:37
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService service;

    @GetMapping("/list")
    public  Object list(Integer id, String name,
                        @RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "10") Integer limit,
                        @RequestParam(defaultValue = "add_time") String sort,
                        @RequestParam(defaultValue = "desc") String order){
        IPage<DtsCategory> list = service.list(id, name, page, limit, sort, order);
        List<DtsCategory> records = list.getRecords();
        long total = list.getTotal();
        HashMap<Object, Object> data = new HashMap<>();
           data.put("total",total);
           data.put("items", records);

        return ResponseUtil.ok(data);
    }

    /**
     *
     * @return
     */
    @RequestMapping("/l1")
    public Object L1(){
        List<CatVo> list = service.list1();
        return ResponseUtil.ok(list);
    };


    @PostMapping("/create")
    public Object create(@RequestBody DtsCategory dtsCategory){
        DtsCategory add = service.add(dtsCategory);

        return  ResponseUtil.ok(add);
    }

    /**
     * 修改
     * @param dtsCategory
     * @return
     */
    @RequestMapping("/update")
    public  Object update(@RequestBody DtsCategory dtsCategory){
        int update = service.update(dtsCategory);
        if(update==0){
            throw new RuntimeException("修改失败");
        }
        return ResponseUtil.ok();

    }

    @PostMapping("/delete")
    public  Object delete(@RequestBody DtsCategory dtsCategory){
        int delete = service.delete(dtsCategory);

        return  ResponseUtil.ok();
    }
}
